/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.gob.pnp.servlets;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;
import pe.gob.pnp.despachadores.TipoDocumentoDespachador;

/**
 *
 * @author Home
 */
public class CargarPlantillaServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try {
           boolean esMultimparte = ServletFileUpload.isMultipartContent(request);
            if (esMultimparte) {

                ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
                List<FileItem> items = servletFileUpload.parseRequest(request);
                for (FileItem item : items) {
                    if (!item.isFormField()) {
                        String nombre = item.getName();
                        int indice = nombre.lastIndexOf("\\");
                        if (indice != -1) {
                            nombre = nombre.substring(indice + 1);
                        }

                        InputStream in = item.getInputStream();
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        int b;
                        byte[] buf = new byte[2048];
                        while ((b = in.read(buf)) != -1) {
                            baos.write(buf);
                        }
                        byte[] data = baos.toByteArray();
                        ByteArrayInputStream bais = new ByteArrayInputStream(data);
                        String valor = "1";//request.getParameter("ntidoid");
                        TipoDocumentoDespachador.guardarPlantilla(bais, data.length, Integer.parseInt(valor));
                        out.println("Se actualizo la plantilla con id:" + valor);
                    }
                }
                BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream()));
                String linea = null;
                while ((linea = in.readLine()) != null) {
                    out.println(linea);
                    System.out.println(linea);
                }
                in.close();

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

